home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / cpphtp2 / code.jar / code / ch07 / fig07_07.txt < prev    next >
Text File  |  1998-02-27  |  682b  |  30 lines

  1. 1   // Fig. 7.7: fig07_07.cpp  
  2. 2   // Using the this pointer to refer to object members.
  3. 3   #include <iostream.h>
  4. 4   
  5. 5   class Test {
  6. 6   public:
  7. 7      Test( int = 0 );             // default constructor
  8. 8      void print() const;
  9. 9   private:
  10. 10     int x;
  11. 11  };
  12. 12  
  13. 13  Test::Test( int a ) { x = a; }  // constructor
  14. 14  
  15. 15  void Test::print() const   // ( ) around *this required
  16. 16  {
  17. 17     cout << "        x = " << x
  18. 18          << "\n  this->x = " << this->x
  19. 19          << "\n(*this).x = " << ( *this ).x << endl;
  20. 20  }
  21. 21  
  22. 22  int main()
  23. 23  {
  24. 24     Test testObject( 12 );
  25. 25  
  26. 26     testObject.print();
  27. 27  
  28. 28     return 0;
  29. 29  }
  30.